/*
 * @Author: DUST
 * @Date: 2023-05-15 15:04:25
 * @LastEditors: DUST
 * @LastEditTime: 2023-05-15 17:21:07
 * @FilePath: \vite-project\vite.config.ts
 * @Description: 
 * undefined
 */
import { PluginOption, UserConfigExport, defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from "@vitejs/plugin-vue-jsx";
import viteConfig from './src/config/viteConfig';
import gzipPlugin from "rollup-plugin-gzip";
import path from "path";
import { AntdResolve, createStyleImportPlugin } from 'vite-plugin-style-import';

// https://vitejs.dev/config/
export default ({ mode } : {
  mode: "dev" | "prod" | "build";
}): UserConfigExport => {
  const userConfig = viteConfig(mode != "dev");
  console.log("当前模式：", mode);
  console.log("当前配置：", userConfig);
  return defineConfig({
    base: "./",
    plugins: [
      vue(),
      vueJsx(),
      gzipPlugin() as PluginOption,
      createStyleImportPlugin({
        resolves: [
          AntdResolve()
        ],
        // libs: [
        //   {
        //     libraryName: "ant-design-vue",
        //     esModule: true,
        //     resolveStyle: name => `ant-design-vue/es/${name}/style/index`
        //     // resolveComponent: name => `ant-design-vue/es/${name}`
        //   }
        // ]
      })
    ],
    server: {
      port: userConfig.developmentPort,
      proxy: {
        "/api": {
          target:  mode == "dev" ? userConfig.baseurl : "",
          changeOrigin: true,
          ws: true,
          rewrite: path => path.replace(/^\/api/, "")
        }
      }
    },
    resolve: {
      alias: [{ find: "@", replacement: path.resolve(__dirname, "./src/") }]
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          modifyVars: {
            "primary-color": userConfig.primaryColor,
            // "link-color": userConfig.LinkColor,
            "border-radius-base": "2px"
          }
        }
      }
    },
    build: {
      rollupOptions: {
        input: {
          main: path.resolve(__dirname, "index.html")
        }
      },
      outDir: userConfig.outDir,
      minify: "terser", // 必须开启：使用terserOptions才有效果
      terserOptions: {
        compress: {
          keep_infinity: true, // 防止 Infinity 被压缩成 1/0，这可能会导致 Chrome 上的性能问题
          drop_console: true, // 生产环境去除 console
          drop_debugger: true // 生产环境去除 debugger
        }
      },
      chunkSizeWarningLimit: 1500 // chunk 大小警告的限制（以 kbs 为单位）
    }
  })
}
